package package1;

import java.io.FileReader;
import java.io.IOException;
	/**
	 * self-defined reader with buffer
	 * the key point is to use 'count' as the index for 'buffer'
	 * while pos stands for the index for all the 'content'
	 * @author Alvin
	 */
public class MyBufferedReader {
	public int count = 0;//pointed to the current count number of object in the buff char (in the method of read(char buff)) 
	public int pos = 0;//pointed to the current index of object that would be taken from the char to write into file
	public final int BUFFER_SIZE = 1024;
	public FileReader fr;
	public char[] buff = new char[BUFFER_SIZE];

	public MyBufferedReader(FileReader fr) {
		super();
		this.fr = fr;
	}

	private int myRead() throws IOException// single read for every byte
	{
		char ch;
		if (count == 0) {
			count = fr.read(buff);//the count returned here shows how many objects are read and stored into the buff 
		}
		if(count < 0)	
			return -1;
		ch = buff[pos];
		count--;
		pos++;
		
		return ch;
	}

	public String myReadLine() throws IOException// return content of one line
	{
		StringBuffer sb = new StringBuffer();
		int ch;
		while((ch = myRead())!=-1){
			if(ch =='\r')
				continue;
			if(ch=='\n')
				return sb.toString();
			sb.append((char)ch);
		}
		return sb.toString();
	}
	public void myClose() throws IOException{
		fr.close();
	}

}
